class LogEntry
	attr_accessor :ip_address, :session_id, :params, :accessed_at, :method, :controller, :action, :done,
		:completion_time_in_secs, :render_time_in_secs, :db_time_in_secs, :http_status
end

log_entries = []
log_entry = nil
File.open("sample.log") do |file|
	file.each_line do |line|
		if line[0..9] == "Processing"
			log_entry = LogEntry.new
			line = line[11..-1]
			log_entry.controller, log_entry.action = line[0..line.index(' ')].strip.split("#")
			line = line[line.index(' ')..-1].strip
			parts = line.split		# gives us ["(for", "11.1.111.11", "at", "2007-10-18", "15:22:51)", "[POST]"]
			log_entry.ip_address = parts[1]
			date = parts[3]
			yr, mo, dy = date.split("-")
			time = parts[4]
			hr, mi, se = time.split(":")
			log_entry.accessed_at = Time.local yr, mo, dy, hr, mi, se
			log_entry.method = parts[5][1..-2]
			print "."
		elsif line.strip[0..10] == "Session ID:"
			log_entry.session_id = line.strip[11..-1].strip
		elsif line.strip[0..10] == "Parameters:"
			log_entry.params = eval(line.strip[11..-1].strip)
		elsif line[0..8] == "Completed"
			parts = line.split("|")
			log_entry.completion_time_in_secs = parts[0][13..parts[0].index("(")-1].strip
			log_entry.render_time_in_secs = parts[1][parts[1].index(":")+1..parts[1].index("(")-1].strip
			log_entry.db_time_in_secs = parts[2][parts[2].index(":")+1..parts[2].index("(")-1].strip
			log_entry.http_status = parts[3].strip.split.first
			log_entry.done = true
			print "+"
		end
		
		if log_entry and log_entry.done
			log_entries << log_entry
			log_entry = nil
		end
	end
end


onip = nil
for le in log_entries.sort_by { |le| [le.ip_address, le.accessed_at] }
	if le.nil?
		puts "was nil"
	else
		if onip != le.ip_address
			onip = le.ip_address
			puts "\tIP: #{le.ip_address}  #{le.session_id}"
		end
		#puts "\tIP: #{le.ip_address}  #{le.session_id}"
		puts "#{le.controller} #{le.action} #{le.accessed_at.strftime('%d/%m/%y %H:%M:%S')}   ||    " + 
		 "#{le.method} #{le.params.keys.join(';')}   ||    " + 
		 "#{le.completion_time_in_secs} #{le.render_time_in_secs} #{le.db_time_in_secs} #{le.http_status}"
	end
end
